﻿using Data;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BusinessLogic
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SanPhamService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select SanPhamService.svc or SanPhamService.svc.cs at the Solution Explorer and start debugging.
    public class SanPhamService : ISanPhamService
    {
        public List<SanPham> DanhSachSanPham(long idloai)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new List<SanPham>();
                var dataTable = sanphamDataTier.DanhSachSanPham(idloai);
                if (dataTable != null)
                {
                    int i = 0;
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        var sp = new SanPham();
                        sp.Id = Convert.ToInt64(dr["S_Id"]);
                        sp.Ten = dr["S_Ten"].ToString();
                        sp.Anh = dr["S_Anh"].ToString();
                        sp.Gia = Convert.ToInt64(dr["S_Gia"]);
                        sp.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        sp.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Cl = ((i % 2 == 0) ? "item_deal2" : "item_deal2 item_deal2_mid");
                        resultList.Add(sp);
                        i++;
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public List<SanPham> TimKiemSanPham(string tukhoa, long giatu, long giaden)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new List<SanPham>();
                var dataTable = sanphamDataTier.TimKiemSanPham(tukhoa, giatu, giaden);
                if (dataTable != null)
                {
                    int i = 0;
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        var sp = new SanPham();
                        sp.Id = Convert.ToInt64(dr["S_Id"]);
                        sp.Ten = dr["S_Ten"].ToString();
                        sp.Anh = dr["S_Anh"].ToString();
                        sp.Gia = Convert.ToInt64(dr["S_Gia"]);
                        sp.Cl = ((i % 2 == 0) ? "item_deal2" : "item_deal2 item_deal2_mid");
                        resultList.Add(sp);
                        i++;
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public List<LoaiSanPham> DanhSachLoaiSanPham()
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new List<LoaiSanPham>();
                var dataTable = sanphamDataTier.DanhSachLoaiSanPham();
                if (dataTable != null)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        var sp = new LoaiSanPham();
                        sp.Id = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Ten = dr["LSP_Ten"].ToString();
                        sp.TrangThai = Convert.ToInt32(dr["LSP_TrangThai"]);
                        sp.SoLuongSP = Convert.ToInt64(dr["LSP_Sl"]);
                        resultList.Add(sp);
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public SanPham ChiTietSanPham(long IdSanPham)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new SanPham();
                var dataTable = sanphamDataTier.ChiTietSanPham(IdSanPham);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        resultList.Id = Convert.ToInt64(dr["S_Id"]);
                        resultList.Ten = dr["S_Ten"].ToString();
                        resultList.Anh = dr["S_Anh"].ToString();
                        resultList.Gia = Convert.ToInt64(dr["S_Gia"]);
                        resultList.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        resultList.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                    }

                }
                var listAnh = new List<SanPhamAnhCT>();
                var listNoiDung = new List<SanPhamNoiDungCT>();
                var dataTableAnh = sanphamDataTier.ChiTietNoiDungSanPham(IdSanPham, 1);
                //var anh1 = new SanPhamAnhCT();
                //anh1.IdSanPham = resultList.Id;
                //anh1.LinkAnh = "a_files/"+resultList.Anh;
                //anh1.Loai = 1;
                //listAnh.Add(anh1);
                if (dataTableAnh != null)
                {
                    foreach (DataRow dr in dataTableAnh.Rows)
                    {
                        var anh = new SanPhamAnhCT();
                        anh.IdSanPham = Convert.ToInt64(dr["S_Id"]);
                        anh.IdChiTiet = Convert.ToInt64(dr["CTS_Id"]);
                        anh.LinkAnh = "chitiet_files/"+dr["CTS_NoiDung"].ToString();
                        anh.Loai = Convert.ToInt32(dr["CTS_Loai"]);
                        listAnh.Add(anh);
                    }
                }
                resultList.lstAnh = listAnh;
                var dataTableND = sanphamDataTier.ChiTietNoiDungSanPham(IdSanPham, 2);
                if (dataTableND != null)
                {
                    foreach (DataRow dr in dataTableND.Rows)
                    {
                        var nd = new SanPhamNoiDungCT();
                        nd.IdSanPham = Convert.ToInt64(dr["S_Id"]);
                        nd.IdChiTiet = Convert.ToInt64(dr["CTS_Id"]);
                        nd.NoiDung = dr["CTS_NoiDung"].ToString();
                        nd.Loai = Convert.ToInt32(dr["CTS_Loai"]);
                        listNoiDung.Add(nd);
                    }
                }
                resultList.lstNoiDung = listNoiDung;

                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string CapNhatLoaiSanPham(LoaiSanPham lsp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatLoaiSanPham(lsp);
            return result;
        }
        public string CapNhatSanPham(SanPham sp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatSanPham(sp);
            return result;

        }
        public string CapNhatSanPhamChiTiet(SanPhamAnhCT anhct,SanPhamNoiDungCT noidungct)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatChiTietSanPham(anhct,noidungct);
            return result;
        }
    }
}
